﻿using Dapper;
using Domain.Entity.ord;
using Domain.IRepository;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EFCore.Repository
{
    public class SaleReturn_dRepository : ISaleReturn_dRepository
    {
        //连接字符串
        string con = "Data Source=121.37.93.71;Initial Catalog=CSSL_ZCTWMS;User ID=sa;Password=geili@2025;Encrypt=False";

        //添加
        public async Task<int> AddSaleReturn_d(SaleReturn_d saleReturn_D)
        {
            using (SqlConnection sqlConnection = new SqlConnection(con))
            {
                await sqlConnection.OpenAsync();
                string sql = $@"insert into [dbo].[ord_sale_return_d](salet_id, saled_id, proc_id, proc_name, saletd_num, saletd_price, saletd_instockOK_num, saletd_instockNO_num, saletd_total, remark)
                                values({saleReturn_D.salet_id},{saleReturn_D.saled_id},{saleReturn_D.proc_id},'{saleReturn_D.proc_name}',{saleReturn_D.saletd_num},'{saleReturn_D.saletd_price}',{saleReturn_D.saletd_instockOK_num},{saleReturn_D.saletd_instockNO_num},'{saleReturn_D.saletd_total}','{saleReturn_D.remark}')";
                return await sqlConnection.ExecuteAsync(sql);
            }
        }

        //删除
        public async Task<int> DeleteSaleReturn_d(int id)
        {
            using (SqlConnection sqlConnection = new SqlConnection(con))
            {
                await sqlConnection.OpenAsync();
                string sql = $"delete from [dbo].[ord_sale_return_d] where saletd_id={id}";
                return await sqlConnection.ExecuteAsync(sql);
            }
        }


        //根据销售退货单与货品获取到主键id
        public async Task<int> Getid(int saletid, int procid)
        {
            using (SqlConnection sqlConnection = new SqlConnection(con))
            {
                await sqlConnection.OpenAsync();
                string sql = $"select saletd_id from [dbo].[ord_sale_return_d] where salet_id={saletid} and proc_id={procid}";
                return await sqlConnection.ExecuteScalarAsync<int>(sql);
            }
        }

        //根据销售退货id查询
        public async Task<List<SaleReturn_d>> GetSaleReturn_Ds(int saletid)
        {
            using (SqlConnection sqlConnection = new SqlConnection(con))
            {
                await sqlConnection.OpenAsync();
                string sql = $"select * from [dbo].[ord_sale_return_d] where salet_id={saletid}";
                return sqlConnection.Query<SaleReturn_d>(sql).ToList();
            }
        }


        //更新入库数
        public async Task<int> UpdateNum(int saletid, int oknum, int nonum)
        {
            using (SqlConnection sqlConnection = new SqlConnection(con))
            {
                await sqlConnection.OpenAsync();
                string sql = $@"update [dbo].[ord_sale_return_d] set saletd_instockOK_num={oknum},saletd_instockNO_num={nonum} where saletd_id={saletid}";
                return await sqlConnection.ExecuteAsync(sql);
            }
        }


        //修改
        public async Task<int> UpdateSaleReturn_d(SaleReturn_d saleReturn_D)
        {
            using (SqlConnection sqlConnection = new SqlConnection(con))
            {
                await sqlConnection.OpenAsync();
                string sql = $@"update [dbo].[ord_sale_return_d] set salet_id={saleReturn_D.salet_id}, saled_id={saleReturn_D.saled_id}, proc_id={saleReturn_D.proc_id}, proc_name='{saleReturn_D.proc_name}', saletd_num={saleReturn_D.saletd_num}, saletd_price='{saleReturn_D.saletd_price}', 
                                saletd_instockOK_num={saleReturn_D.saletd_instockOK_num}, saletd_instockNO_num={saleReturn_D.saletd_instockNO_num}, saletd_total='{saleReturn_D.saletd_total}', remark='{saleReturn_D.remark}' where saletd_id={saleReturn_D.saletd_id}";
                return await sqlConnection.ExecuteAsync(sql);
            }
        }
    }
}
